package com.htjc.integral.conversion.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.htjc.integral.conversion.mapper.EcExtJfWsdlMapper;
import com.htjc.integral.conversion.mapper.EcMarketingAddressMapper;
import com.htjc.integral.conversion.service.IGdtService;
import com.htjc.integral.conversion.util.XflUtil;
import com.htjc.integral.conversion.util.XmlConverUtil;
import com.htjc.integral.model.conversion.EcExtJfWsdl;
import com.htjc.integral.model.conversion.EcMarketingAddress;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "conversionLogger")
@Service
public class GdtServiceImpl implements IGdtService {

	@Autowired
	private EcExtJfWsdlMapper wsdlMapper;
	
	@Autowired
	private EcMarketingAddressMapper marketingAddressMapper;
	
	//111
	@Override
	public Map<String, Object> queryScoreDetail (String consNo, String orgNo) {
		log.error("bsc中queryScoreDetail方法进来后参数，网省编码：" + orgNo +",用户编号：" + consNo);
		String deOrgNo = orgNo;
		String noencode_consNo=consNo;
		String noencode_orgNo=orgNo;
		Map<String,Object> mReturn = new HashMap<String,Object>();
		EcMarketingAddress marketingAddress = this.marketingAddressMapper.selectByPrimaryKey(orgNo);
		String fromxml="";
		String toxml="";
		Map<String,String> map  =new HashMap<String, String>();
		if(marketingAddress != null)
			orgNo = StringUtils.isBlank(marketingAddress.getCodeExt()) ? orgNo :marketingAddress.getCodeExt();
		EcExtJfWsdl jfWsdl = this.wsdlMapper.getJfWsdl(deOrgNo, "jfmxcx");
		if(jfWsdl != null){
			log.error("queryScoreDetail接口请求配置信息："+jfWsdl.toString());
			String wsdlurl = jfWsdl.getWsdlAddress();//请求wsdlurl
			String wsdlHeader = jfWsdl.getZylzd1();//请求头
			//请求WSDL
			log.error("********请求WebServicel...START："+new Date() +";");
			
			String inputXml="";
			//浙江的请求参数与别的网省不一样，单独处理
			if("330000".equals(deOrgNo)){
				Map wheremap=new HashMap<String, String>();
				wheremap.put("consNo", consNo);
				wheremap.put("orgNo", deOrgNo);
				List<Map> uslist=this.marketingAddressMapper.getUserBybindInfo(wheremap);
				String acctId="";
				String userName="";
				
				if(uslist.size()>0){
					Map usmap=uslist.get(0);
					acctId=String.valueOf(usmap.get("PARTNERID"));
					userName=String.valueOf(usmap.get("LOGIN_ACCOUNT"));
					userName=userName.length()>256?userName.substring(0,256):userName;
				}
				/*inputXml ="<ORDER><srvCode>0223003</srvCode><channelCode>ESGCC</channelCode><acctId>"+acctId+"</acctId><userName>"+userName+"</userName><orgNo>" + orgNo + "</orgNo>" +
						"<consNo>"+ consNo +"</consNo></ORDER>";*/
				inputXml ="<DBSET><R><C N=\"srvCode\">LSSP_000180</C>"
						+ "<C N=\"channelCode\">ESGCC</C><C N=\"acctId\">"+acctId+"</C><C N=\"userName\">"+userName
						+"</C><C N=\"consNo\">"+consNo+"</C><C N=\"orgNo\">"+orgNo+"</C></R></DBSET>";
			}else{
				//其他省份操作
				
				inputXml ="<ORDER><servicecode>0223003</servicecode><source>esgcc</source><target>" + orgNo + "</target>" +
						"<data><consNo>"+ consNo +"</consNo><orgNo>"+ orgNo +"</orgNo></data></ORDER>";
				log.error("+++++++++++++++++++++GdtServiceImpl--->queryScoreDetail： 进入非浙江网省接口调用：inputXml="+inputXml);
				
			}
			String input = "<![CDATA[?]]>".replace("?", inputXml);
			log.error("********queryScoreDetail方法请求拼接的参数xml："+input+";");
//			 map  = SoaClient.requestSoa(wsdlurl, wsdlHeader, input, "?", "", "UTF-8",jfWsdl.getZylzd2());
			//===========================dxp001=====================================
			
	    	String success = "false";
			String result = "";
			if(map.get("success")!=null){
				success = map.get("success");
			}
			if(map.get("result")!=null){
				result = map.get("result");
			}
			log.error("queryScoreDetail方法调用dxp后返回的结果：" + result);
			if(StringUtils.isNotBlank(result) && success.equals("true")){
				
				if("330000".equals(deOrgNo)){
					//result=XmlConverUtil.zhejiangStrFormat(result);
					try {
						List<Map> listr=XflUtil.toList("<DBSET><R>"+result+"</R></DBSET>");
						if(listr.size()>0){
							mReturn=listr.get(0);
							//处理如果的rtnCode的返回值0表示成功，1表失败
							if(mReturn.containsKey("rtnCode")){
								String rtnCode=String.valueOf(mReturn.get("rtnCode"));
								if("0".equals(rtnCode)){
									mReturn.put("code", "1");
								}else if("1".equals(rtnCode)){
									mReturn.put("code", "0");
								}
							}
							//处理积分返回结果，去掉小数
							if(mReturn.containsKey("score")){
								String score=String.valueOf(mReturn.get("score"));
								score=score.indexOf(".")>-1?score.substring(0,score.indexOf(".")):score;
								if("".equals(score)||null==score){
									score="0";
								}
								mReturn.put("score", score);
							}
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else{
					if(result.indexOf("<root>")<0 || result.indexOf("<ROOT>")<0)
					{
						result="<root>"+result+"</root>";
					}
					log.error("XmlConverUtil.xmltoMap2(result)--result:"+result);
					mReturn = XmlConverUtil.xmltoMap2(result);
				}
				mReturn.put("orgNo", deOrgNo);
				
				log.error("XmlConverUtil.xmltoMap2(result)--mReturn:"+mReturn.toString());
			}	
			//未进行接口转换配置的省份操作
			//20180131删除95598调用接口
		}else{
//			    log.error("95598接口调了=======");
//				//河南
//			    if(orgNo.equals("410000")){
//			    	orgNo = "41101";
//			    }
//			    //四川
//			    if(orgNo.equals("510000")){
//			    	orgNo = "51101";
//			    }
//			    //山西
//			    if(orgNo.equals("140000")){
//			    	orgNo = "14101";
//			    }
//			    //江苏
//			    if(orgNo.equals("320000")){
//			    	orgNo = "32101";
//			    }
//			    log.error("orgNo12++++++++++++++++++"+orgNo);
//			    log.error("consNo12++++++++++++++++++"+consNo);
//				consNo = RSAServer.rsaEncryption(consNo);
//				orgNo = RSAServer.rsaEncryption(orgNo);
//				log.error("consNo34++++++++++++++++++"+consNo);
//				log.error("orgNo34++++++++++++++++++"+orgNo);
//				String url = SgccUtils.getConfValue("csups.95598.scoreDetail.request.url");
//				log.error("数据库获取url++++++++++++++++++"+url);
//				if(url==null){
//					url = "http://210.77.179.11:7012/CSUPS/95598/weixin/queryScoreDetail";
//				}
		//		String url = "http://210.77.179.14:7016/CSUPS/95598/weixin/queryScoreDetail";
		//		String url = "http://210.77.179.11:7012/CSUPS/95598/weixin/queryScoreDetail";
				//            http://210.77.179.14:7016/CSUPS/95598/weixin/queryScoreDetail
//				if(StringUtils.isBlank(consNo)||StringUtils.isBlank(orgNo)){
//					log.error("是否为空++++++++++++++++++"+consNo+",,,"+orgNo);
//					mReturn.put("score", "0");
//					mReturn.put("consNo", consNo);
//					mReturn.put("orgNo", deOrgNo);
//					return mReturn;
//				}else{
//					//去空
//					consNo = consNo.trim();
//					orgNo = orgNo.trim();
//					log.error("不为空++++++++++++++++++"+consNo+",,,"+orgNo);
//				}
//				LinkedHashMap<String, String> reqParams = new LinkedHashMap<String, String>();
//				reqParams.put("consNo", consNo);
//				reqParams.put("orgNo", orgNo);
//				reqParams.put("provinceNo", orgNo);
//				log.error("11URL++++++++++++++++++"+url);
//				try
//				{
//					String requestData = HttpClientTools.getInstance().includeByPostTo60(url, reqParams);
//					if(!requestData.isEmpty())
//					{
//					log.error("积分明细信息查询接口："+requestData);
//					JSONObject json = (JSONObject) JSONSerializer.toJSON(requestData);
//					mReturn = JsonUtil.getInstance().json2Map(json);
//					if(mReturn != null && mReturn.size() != 0){
//						log.error("进来了吧========");
//						 String score = RSAServer.rsaDecrypt(String.valueOf(mReturn.get("score")));
//						consNo = RSAServer.rsaDecrypt(String.valueOf(mReturn.get("consNo")));
//						//orgNo = RSAServer.rsaDecrypt(String.valueOf(mReturn.get("orgNo")));
//						mReturn.put("score", score);
//						mReturn.put("consNo", consNo);
//						mReturn.put("orgNo", deOrgNo);
//					}
//					else
//					{
//						log.error("mReturn为空========");
//						mReturn.put("score", "0");
//						mReturn.put("consNo", noencode_consNo);
//						mReturn.put("orgNo", noencode_orgNo);
//					}
//					}
//					else
//					{
//						log.error("requestData为空========");
//						mReturn.put("score", "0");
//						mReturn.put("consNo", noencode_consNo);
//						mReturn.put("orgNo", noencode_orgNo);
//					}
//				}catch (Exception e) {
//					mReturn.put("score", "0");
//					mReturn.put("consNo", noencode_consNo);
//					mReturn.put("orgNo", noencode_orgNo);
//					e.printStackTrace();
//					log.error("##############GdtServiceImpl-->queryScoreDetail" + mReturn.toString());
//				}
			mReturn.put("score", "0");
			mReturn.put("consNo", consNo);
			mReturn.put("orgNo", deOrgNo);
		}
		if(map.get("fromxml")!=null)
		{
			mReturn.put("fromxml", map.get("fromxml").toString());
		}
		if(map.get("toxml")!=null)
		{
			mReturn.put("toxml", map.get("toxml").toString());
		}
		log.error("##############网省积分转换最终结果:" + mReturn.toString());
		return mReturn;
	}
	
	//111
	@Override
	public Map<String, Object> queryScoreConvert(String consNo, String orgNo,
			String score,String goldCoin,String convertTime,String id) {
		String deOrgNo = orgNo;
		String input ="";
		String result = "";
		String toxml = "";
		Map<String,Object> mReturn = new HashMap<String,Object>();
		Map<String,String> map =new HashMap<String,String>();
		EcMarketingAddress marketingAddress = this.marketingAddressMapper.selectByPrimaryKey(orgNo);
		if(marketingAddress != null)
			orgNo = StringUtils.isBlank(marketingAddress.getCodeExt()) ? orgNo :marketingAddress.getCodeExt();
		EcExtJfWsdl jfWsdl = this.wsdlMapper.getJfWsdl(deOrgNo, "jfzh");
		if(jfWsdl != null){
			log.error("queryScoreConvert接口请求配置信息："+jfWsdl.toString());
			String wsdlurl = jfWsdl.getWsdlAddress();//请求wsdlurl
			String wsdlHeader = jfWsdl.getZylzd1();//请求头
			//请求WSDL
			log.error("********请求WebServicel...START："+new Date() +";");
			String inputXml="";
			//浙江的请求参数与别的网省不一样，单独处理
			if("330000".equals(deOrgNo)){
				Map wheremap=new HashMap<String, String>();
				wheremap.put("consNo", consNo);
				wheremap.put("orgNo", deOrgNo);
				List<Map> uslist=this.marketingAddressMapper.getUserBybindInfo(wheremap);
				String acctId="";
				String userName="";
				
				if(uslist.size()>0){
					Map usmap=uslist.get(0);
					acctId=String.valueOf(usmap.get("PARTNERID"));
					userName=String.valueOf(usmap.get("LOGIN_ACCOUNT"));
					userName=userName.length()>256?userName.substring(0,256):userName;
				}
				/*inputXml ="<ORDER><srvCode>0223003</srvCode><channelCode>ESGCC</channelCode><acctId>"+acctId+"</acctId><userName>"+userName+"</userName><orgNo>" + orgNo + "</orgNo>" +
						"<consNo>"+ consNo +"</consNo></ORDER>";*/
				inputXml ="<DBSET><R><C N=\"srvCode\">LSSP_000181</C>"
						+ "<C N=\"channelCode\">ESGCC</C><C N=\"acctId\">"+acctId+"</C><C N=\"userName\">"+userName
						+"</C><C N=\"consNo\">"+consNo+"</C><C N=\"orgNo\">"+orgNo+"</C>"
								+ "<C N=\"score\">"+score+"</C><C N=\"goldCoin\">"+goldCoin+"</C><C N=\"convertTime\">"+convertTime+"</C><C N=\"id\">"+id+"</C>"
								+ "</R></DBSET>";
			}else{
				inputXml ="<ORDER><servicecode>0223004</servicecode><source>esgcc</source><target>" + orgNo + "</target>" +
						"<data><consNo>"+ consNo +"</consNo><orgNo>"+ orgNo +"</orgNo><score>"+ score +"</score><goldCoin>"+ goldCoin +"</goldCoin><convertTime>"+ convertTime +"</convertTime><id>"+ id +"</id></data></ORDER>";
					
			}
			input= "<![CDATA[?]]>".replace("?", inputXml);
			log.error("********queryScoreConvert方法请求拼接的参数xml："+input+";");
//			map= SoaClient.requestSoa(wsdlurl, wsdlHeader, input, "?", "", "UTF-8",jfWsdl.getZylzd2());
			//===========================dxp=====================================
			
	    	String success = "false";
			
			if(map.get("success")!=null){
				success = map.get("success");
			}
			if(map.get("result")!=null){
				result = map.get("result");
			}
			log.error("queryScoreConvert方法调用dxp后返回的结果：" + result);
			if(StringUtils.isNotBlank(result) && success.equals("true")){
				if("330000".equals(deOrgNo)){
					//result=XmlConverUtil.zhejiangStrFormat(result);
					try {
						List<Map> listr=XflUtil.toList("<DBSET><R>"+result+"</R></DBSET>");
						if(listr.size()>0){
							mReturn=listr.get(0);
							//处理如果的rtnCode的返回值0表示成功，1表失败
							if(mReturn.containsKey("rtnCode")){
								String rtnCode=String.valueOf(mReturn.get("rtnCode"));
								if("0".equals(rtnCode)){
									mReturn.put("code", "1");
								}else if("1".equals(rtnCode)){
									mReturn.put("code", "0");
								}
							}
							//处理积分返回结果，去掉小数
							if(mReturn.containsKey("score")){
								String score1=String.valueOf(mReturn.get("score"));
								score1=score1.indexOf(".")>-1?score1.substring(0,score1.indexOf(".")):score1;
								if("".equals(score1)||null==score1){
									score1="0";
								}
								mReturn.put("score", score1);
							}
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else{
					if(result.indexOf("<root>")<0 || result.indexOf("<ROOT>")<0)
					{
						result="<root>"+result+"</root>";
					}
					log.error("GdtServiceImpl-->queryScoreConvertForNew-->XmlConverUtil.xmltoMap2(result)"+result);
					mReturn = XmlConverUtil.xmltoMap2(result);
				}
			}
			//20180131删除95598接口调取内容
		}else{
			
//			consNo = RSAServer.rsaEncryption(consNo);
//			orgNo = RSAServer.rsaEncryption(orgNo);
//			score = RSAServer.rsaEncryption(score);
//			goldCoin = RSAServer.rsaEncryption(goldCoin);
//			convertTime = RSAServer.rsaEncryption(convertTime);
//			id = RSAServer.rsaEncryption(id);
//			String url = SgccUtils.getConfValue("csups.95598.scoreConvert.request.url");
//	//		String url = "http://210.77.179.14:7016/CSUPS/95598/weixin/scoreTransfer";
//			if(StringUtils.isBlank(consNo)||StringUtils.isBlank(orgNo)
//					||StringUtils.isBlank(score)||StringUtils.isBlank(goldCoin)
//					||StringUtils.isBlank(convertTime)||StringUtils.isBlank(id)){
//				return null;
//			}else{
//				//去空
//				consNo = consNo.trim();
//				orgNo = orgNo.trim();
//				score = score.trim();
//				goldCoin = goldCoin.trim();
//				convertTime = convertTime.trim();
//				id = id.trim();
//			}
//			LinkedHashMap<String, String> reqParams = new LinkedHashMap<String, String>();
//			reqParams.put("consNo", consNo);
//			reqParams.put("orgNo", orgNo);
//			reqParams.put("score", score);
//			reqParams.put("goldCoin", goldCoin);
//			reqParams.put("id", id);
//			reqParams.put("convertTime", convertTime);
//			reqParams.put("provinceNo", orgNo);
//			log.error(url);
//			String requestData = HttpClientTools.getInstance().includeByPostTo60(url, reqParams);
//			log.error("积分转换接口返回数据："+requestData);
//			mReturn.put("toxml", requestData);
//			JSONObject json = (JSONObject) JSONSerializer.toJSON(requestData);
//			mReturn = JsonUtil.getInstance().json2Map(json);
//			if(mReturn != null && mReturn.size() != 0){
//				String code = RSAServer.rsaDecrypt(String.valueOf(mReturn.get("code")));
//				mReturn.put("code", code);
//			}
		}
		log.error("bsc中queryScoreConvert方法处理后返回结果map:" + mReturn.toString());
		
		mReturn.put("fromxml", map.get("fromxml"));
		mReturn.put("toxml", map.get("toxml"));
		return mReturn;
	}
	
}
